package com.telit.firecommand.feign.factory;

import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.common.util.LanguageUtils;
import com.telit.firecommand.feign.FileFeignClient;
import feign.FeignException;
import feign.Response;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * windows file 服务熔断工厂
 *
 * @author yuezhuang
 * @date 2022-10-20
 */
@Slf4j
@Component
public class FileFeignClientFallbackFactory implements FallbackFactory<FileFeignClient> {

    /**
     * Returns an instance of the fallback appropriate for the given cause
     *
     * @param cause corresponds to {@link com.netflix.hystrix#getExecutionException()}
     *              often, but not always an instance of {@link FeignException}.
     */
    @Override
    public FileFeignClient create(Throwable cause) {
        return new FileFeignClient() {

            /**
             * 标准化输出微服务内部调用错误信息
             *
             * @return DataReturn
             */
            private DataReturn defaultFallback() {
                String msg = LanguageUtils.codeMessage(ResultCode.SERVER_FEIGN_ERROR.getCode(), cause.getMessage());
                log.error(msg, cause);
                return DataReturn.failure(ResultCode.SERVER_FEIGN_ERROR.getCode(), msg);
            }

            @Override
            public DataReturn downloadVideo(String userId, String cameraIndexCode, String beginTime, String endTime,
                                            String recordLocation, String callback) {
                return defaultFallback();
            }

            @Override
            public DataReturn queryIsDownloadByCameraId(String userId, String cameraIndexCode) {
                return defaultFallback();
            }

            @Override
            public Response download(String fileName) {
                return null;
            }

            @Override
            public DataReturn delete(String fileName) {
                return defaultFallback();
            }
        };
    }

}
